$(function () {
    var form = layui.form
    var laypage = layui.laypage
    var pagenum = 1
    var pagesize = 3
    function loadUserList (param) {
      console.log(param)
      $.ajax({
        type: 'get',
        url: 'admin/users',
        data: param,
        success: function (res) {
          var tags = template('table-tpl', res)
          $('.layui-table tbody').html(tags)
          laypage.render({
            elem: 'articlePage',
            curr: pagenum,
            count: res.total,
            limit: pagesize,
            limits: [3, 10, 30, 40, 100],
            layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
            jump: function (obj, first) {
              pagenum = obj.curr
              pagesize = obj.limit
              if (!first) {
                loadUserList({
                  pagenum: pagenum,
                  pagesize: pagesize
                })
              }
            }
          });
        }
      })
    }
    loadUserList({
      pagenum: pagenum,
      pagesize: pagesize
    })
    $('.layui-table tbody').on('click', '.layui-btn-danger', function (e) {
      var id = $(e.target).data('id')
      layer.confirm('确认要删除用户吗？', function (index) {
        $.ajax({
          type: 'delete',
          url: 'admin/users/' + id,
          success: function (res) {
            layer.msg(res.message)
            loadUserList({
              pagenum: pagenum,
              pagesize: pagesize
            })
          }
        })
      })
    })
    $('.layui-table tbody').on('click', '.layui-btn-normal', function (e) {
      var id = $(e.target).data('id')
      var index = layer.open({
        type: 1,
        title: '重置密码',
        content: $('#repwd-form-tpl').html(),
        area: ['500px', '250px']
      })
      // 重置密码
      $('#repwd-form').submit(function (e) {
        e.preventDefault()
        $.ajax({
          type: 'put',
          url: 'admin/users/' + id,
          data: {
            password: $('#repwd-form input[name=password]').val()
          },
          success: function (res) {
            layer.msg(res.message)
            layer.close(index)
          }
        })
      })
    })
    
  })